package com.atguigu.crud.controller;

import com.atguigu.crud.bean.Employee;
import com.atguigu.crud.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeService employeeService;
    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    /*
    插入员工
     */
    public ModelAndView insert(Employee employee){
        ModelAndView modelAndView=new ModelAndView();

       employeeService.saveEmp(employee);
        System.out.println(employee.toString());
        return  modelAndView;
    }
/*
修改员工信息
 */
    @RequestMapping(value = "/update/{empId}",method = RequestMethod.GET)
    public ModelAndView update(@PathVariable("empId")  Integer empId){
        ModelAndView modelAndView=new ModelAndView();
        Employee employee=new Employee();
        employee=employeeService.getEmp(empId);
      // employee=  employeeService.updateEmp(empId);
     System.out.println(employee.toString());
      modelAndView.addObject("emp",employee);
        modelAndView.setViewName("index");
        return  modelAndView;
    }
        /*
        删除
         */
        public int del(Integer empId){
            employeeService.deleteEmp(empId);
            return  1;

        }
    /*
    分页查询数据
     */
    @RequestMapping("/emps.do")
    public ModelAndView query2(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model){
        PageHelper.startPage(pn,5);
        List<Employee> list=employeeService.getAllWithDept();
        PageInfo pageInfo=new PageInfo(list,3);
        model.addAttribute("pageInfo",pageInfo);
        pageInfo.getNavigatepageNums();
        List<Employee> list2 = pageInfo.getList();
        for (Employee employee : list2) {
           // System.out.println("ID："+employee.getEmpId()+"==>Name:"+employee.getEmpName()+"deptname:"+employee.getDepartment());
        }
        ModelAndView m=new ModelAndView();
        m.setViewName("index");
        return m;


    }
}
